<template>
	<view class="pages">
		<view class="couponList">
			<view class="item">
				<view class="left">
					<view class="face_value"><text>￥</text>{{couponList.face_value}}</view>
					<view class="full">满{{couponList.full}}元可用</view>
				</view>
				<view class="right">
					<view class="title">unclearcare 专享优惠券</view>
					<view class="total">剩余: {{couponList.total - couponList.receive_num}}张</view>
					<view class="time">有效期: 领取后{{couponList.validity_time}}天有效</view>
				</view>
				<button :disabled="disabled" @click="getCoupon(couponList.id)">{{text}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {HTTP} from '@/pages/http.js';
	let http = new HTTP();
	export default {
		data(){
			return {
				text: '点击领取',
				couponList: {},
				disabled: ''
			}
		},
		onLoad(e) {
			this.getCouponInfo(e.scene)
		},
		methods:{
			getCouponInfo(id){
				uni.showLoading({
					title: '加载中..'
				})
				http.request({
					data:{
						id
					},
					url: 'coupon/detail',
					success: (res)=>{
						uni.hideLoading();
						if(res.data.code == 200){
							this.couponList = res.data.data;
						}
					}
				})
			},
			getCoupon(id){
				http.request({
					method: 'POST',
					url: 'coupon/receive',
					data:{
						id
					},
					success:(res)=>{
						if(res.data.code == 200){
							uni.showToast({
								title: res.data.msg
							})
							this.disabled = 'disabled';
							this.text = '已领取';
							setTimeout(()=>{
								uni.switchTab({
									url: '/pages/index/index'
								})
							},2000)
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f5f5f9;
		height: 100%;
	}
	.pages{
		background-color: #f5f5f9;
		height: 100%;
		.couponList{
			padding: 30upx;
			.item{
				position: relative;
				display: flex;
				width: 100%;
				height: 140upx;
				background: url(https://xx.nikm.cn/upload/coupon_bg.png) no-repeat;
				background-size: 100%;
				margin-bottom: 20upx;
				.left{
					width: 176upx;
					height: 100%;
					color: #fff;
					padding-top: 15upx;
					.face_value{
						font-size: 56upx;
						font-weight: bold;
						text-align: center;
						text{
							font-size: 30upx;
							font-weight: 200;
						}
					}
					.full{
						font-size: 18upx;
						text-align: center;
					}
				}
				.right{
					width: 340upx;
					height: 100%;
					padding: 20upx 0 0 30upx;
					.title{
						font-size: 28upx;
						margin-bottom: 5upx;
					}
					.total{
						font-size: 22upx;
					}
					.time{
						font-size: 22upx;
					}
				}
				button{
					position: absolute;
					right: 30upx;
					top: 45upx;
					width: 130upx;
					height: 50upx;
					line-height: 51upx;
					text-align: center;
					color: #fff;
					background-color: #000;
					border-radius: 25upx;
				}
				button[disabled] {
					color: #fff !important;
					background: #ccc !important;
					border: none !important;
				}
			}
		}
	}
</style>
